package com.hello.system.controller;

import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.system.service.IWxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 微信支付控制器
 * 
 * @author hello
 */
@RestController
@RequestMapping("/wxpay")
@Slf4j
public class WxPayController extends BaseController {

    @Autowired
    private IWxPayService wxPayService;
    
    /**
     * 创建支付订单
     */
    @PostMapping("/createorder")
    public AjaxResult createOrder(@RequestBody Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        BigDecimal amount = new BigDecimal(params.get("amount").toString());
        String description = (String) params.get("description");
        String openid = (String) params.get("openid");
        String clientIp = (String) params.get("clientIp"); // 可选参数：客户端IP地址
        
        if (orderId == null || orderId.isEmpty()) {
            return AjaxResult.error("订单号不能为空");
        }
        
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("支付金额必须大于0");
        }
        
        if (description == null || description.isEmpty()) {
            return AjaxResult.error("商品描述不能为空");
        }
        
        if (openid == null || openid.isEmpty()) {
            return AjaxResult.error("openid不能为空");
        }
        
        try {
            Map<String, Object> payInfo;
            if (clientIp != null && !clientIp.isEmpty()) {
                payInfo = wxPayService.createOrder(orderId, amount, description, openid, clientIp);
            } else {
                payInfo = wxPayService.createOrder(orderId, amount, description, openid);
            }
            return AjaxResult.success("创建支付订单成功", payInfo);
        } catch (Exception e) {
            log.error("创建支付订单失败", e);
            return AjaxResult.error("创建支付订单失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询订单
     */
    @GetMapping("/queryorder/{orderId}")
    public AjaxResult queryOrder(@PathVariable String orderId) {
        if (orderId == null || orderId.isEmpty()) {
            return AjaxResult.error("订单号不能为空");
        }
        
        try {
            Map<String, Object> orderInfo = wxPayService.queryOrder(orderId);
            return AjaxResult.success("查询订单成功", orderInfo);
        } catch (Exception e) {
            log.error("查询订单失败", e);
            return AjaxResult.error("查询订单失败: " + e.getMessage());
        }
    }
    
    /**
     * 支付结果通知
     */
    @PostMapping("/notify")
    public String payNotify(@RequestBody String notifyData) {
        log.info("收到微信支付结果通知");
        return wxPayService.parseOrderNotification(notifyData);
    }
    
    /**
     * 申请退款
     */
    @PostMapping("/refund")
    public AjaxResult refund(@RequestBody Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        String refundId = (String) params.get("refundId");
        BigDecimal amount = new BigDecimal(params.get("amount").toString());
        BigDecimal totalAmount = new BigDecimal(params.get("totalAmount").toString());
        String reason = (String) params.get("reason");
        
        if (orderId == null || orderId.isEmpty()) {
            return AjaxResult.error("订单号不能为空");
        }
        
        if (refundId == null || refundId.isEmpty()) {
            return AjaxResult.error("退款单号不能为空");
        }
        
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("退款金额必须大于0");
        }
        
        if (totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("原订单金额必须大于0");
        }
        
        try {
            Map<String, Object> refundInfo = wxPayService.refund(orderId, refundId, amount, totalAmount, reason);
            return AjaxResult.success("申请退款成功", refundInfo);
        } catch (Exception e) {
            log.error("申请退款失败", e);
            return AjaxResult.error("申请退款失败: " + e.getMessage());
        }
    }
} 